package com.paishen.peiniwan.module.set;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.paishen.common.dialog.LoadingDialog;
import com.paishen.common.util.BaseUtil;
import com.paishen.peiniwan.App;
import com.paishen.peiniwan.BaseActivity;
import com.paishen.peiniwan.C;
import com.paishen.peiniwan.R;
import com.paishen.peiniwan.frame.PnwOkCallbackAdapter;
import com.paishen.peiniwan.frame.http.HttpResp;
import com.paishen.peiniwan.frame.http.PnwHttpManager;
import com.paishen.common.widget.TitleLayout;
import com.squareup.okhttp.Request;

public class SetFeedbackActivity extends BaseActivity {

    private Button btnSubmit;
    private EditText edContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_feedback);

        TitleLayout layTitle = (TitleLayout) findViewById(R.id.lay_title);
        layTitle.pushContent(makeBackView(), getString(R.string.set_feedback), null, false);

        edContent = (EditText) findViewById(R.id.ed_content);

        btnSubmit = (Button) findViewById(R.id.btn_submit);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseUtil.hideSoftInputFromWindowAlways(SetFeedbackActivity.this);
                if (checkFeedbackContent(true)) {
                    requestAdd();
                }
            }
        });

        findViewById(R.id.lay_container).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseUtil.hideSoftInputFromWindowAlways(SetFeedbackActivity.this);
            }
        });
    }

    private final void requestAdd() {
        String title = "Android" + getString(R.string.set_feedback);
        String content = edContent.getText().toString();
        String nickname = App.i().getUser().getNickname();

        final LoadingDialog dlgLoading = new LoadingDialog.Builder(this).create();
        dlgLoading.show();
        PnwHttpManager.Feedback.add(TAG, title, content, nickname, new PnwOkCallbackAdapter<HttpResp>() {
            @Override
            public void onResponse(HttpResp response, boolean fresh) {
                super.onResponse(response, fresh);
                dlgLoading.dismiss();
                if (response.errcode == C.http.RESULT_SUCCESS) {
                    BaseUtil.showToast(R.string.set_feedback_success);
                    btnSubmit.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    }, 1600);
                } else if (fresh && response.errcode != C.http.RESULT_LOGIN_EXPIRE &&
                        response.errcode != C.http.RESULT_LOGIN_OFFLINE){
                    BaseUtil.showToast(response.errmsg);
                }
            }

            @Override
            public void onFailure(Request request, Exception e) {
                super.onFailure(request, e);
                dlgLoading.dismiss();
            }
        });
    }

    private final boolean checkFeedbackContent(boolean remind) {
        boolean result = !TextUtils.isEmpty(edContent.getText());
        if (!result && remind) {
            BaseUtil.showToast(R.string.set_input_feedback);
        }
        return result;
    }
}
